package client;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.Spatial.CullHint;

public class ViewPortState extends AbstractAppState {
    private Camera g_cam = new Camera();
    private ViewPort g_view;
    private Node g_root = new Node();
    Spatial hands;
    Spatial handsnode;
    DirectionalLight g_light = new DirectionalLight();
 
    public ViewPortState()  {
    }
 
    public Node getRoot() {
        return g_root;
    }
 
    public Camera getCamera() {
        return g_cam;
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application app)
    {
        g_root = new Node( "guiview Root" );
        g_root.setCullHint(CullHint.Never);
        Camera originalCam = app.getCamera();
        g_cam = new Camera(originalCam.getWidth(), originalCam.getHeight());
        g_cam.setFrustumFar(1000f);
        g_view = app.getRenderManager().createMainView("guiview", g_cam);
        g_view.setEnabled(true);
        g_view.setClearFlags(false, true, false);
        g_view.attachScene( g_root );
        g_light.setDirection( new Vector3f(-.90f, -.90f, -.65f) );
        g_light.setColor(ColorRGBA.White);
        g_root.addLight(g_light);
        g_root.updateLogicalState(1);
        g_root.updateGeometricState();
        createHands((SimpleApplication)app);
    }
    
    public void createHands(SimpleApplication app)
    {
        Camera view = new Camera(app.getCamera().getHeight(),app.getCamera().getWidth());;
        hands = Main.appref.getAssetManager().loadModel("Models/hands.j3o");
        hands.scale(1f);
        handsnode = Main.spawning.createModel(Main.appref, new Vector3f(0,0,0), hands, "01ddf");
        app.getStateManager().getState(ViewPortState.class).getCamera().lookAt(handsnode.getLocalTranslation(), new Vector3f(0,0,0));
        g_root.attachChild(handsnode);
    }
    
    public void alignLights()
    {
        //g_light.setDirection(Main.player.getLocation().subtract(Main.lights.playerlight.getDirection().normalizeLocal()).add(Main.player.getLook().subtract(Main.lights.playerlight.getDirection())));
    }
 
    @Override
    public void render(RenderManager rm) {
        g_root.updateGeometricState();
    }
 
    @Override
    public void update( float tpf ) {
        g_root.updateLogicalState(tpf);
    }
 
}